import React,{useEffect} from 'react'
import "./App.css"
import RouteList,{ AuthRoute } from "./router"

import { useLocation, useHistory } from 'react-router-dom'

import { getStorage } from "./utils/LocalStorage"

//做登录验证，如果在localStorage中如果有username证明已经登录，否则则没有登录，跳转登录页
const App = () => {

  
  const location = useLocation();
  const history = useHistory();


  function checkLogin(path){
    if(path!=="/login"){
      if(getStorage("username")||getStorage("token")){
        return false
      }else{
          history.push("/login")
      }
    }else{
      return false
    }
  }

  useEffect(() => {
    checkLogin(location.pathname)
    history.listen(location=>{
      checkLogin(location.pathname)
    })
  }, []);


  return (
    <>
      <RouteList />
      <AuthRoute />
    </>
  );
}

export default App;
